﻿Namespace DoFactory.GangOfFour.State.Structural


    ''' <summary>
    ''' MainApp startup class for Structural
    ''' State Design Pattern.
    ''' </summary>
    Public Class MainApp


        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()

            ' Setup context in a state
            Dim c As New Context(New ConcreteStateA())
            ' Issue requests, which toggles state
            c.Request()
            c.Request()
            c.Request()
            c.Request()

            ' Wait for user
            'Console.ReadKey()

        End Sub

    End Class

    ''' <summary>
    ''' The 'State' abstract class
    ''' </summary>
    MustInherit Class State

        Public MustOverride Sub Handle(ByVal context As Context)

    End Class

    ''' <summary>
    ''' A 'ConcreteState' class
    ''' </summary>
    Class ConcreteStateA
        Inherits State

        Public Overrides Sub Handle(ByVal context As Context)
            context.State = New ConcreteStateB()
        End Sub

    End Class

    ''' <summary>
    ''' A 'ConcreteState' class
    ''' </summary>
    Class ConcreteStateB
        Inherits State

        Public Overrides Sub Handle(ByVal context As Context)
            context.State = New ConcreteStateA()
        End Sub

    End Class

    ''' <summary>
    ''' The 'Context' class
    ''' </summary>
    Class Context

        Private _state As State

        ' Constructor
        Public Sub New(ByVal state As State)
            Me.State = state
        End Sub

        ' Gets or sets the state
        Public Property State() As State
            Get
                Return _state
            End Get
            Set(ByVal value As State)
                _state = value
                Console.WriteLine("State: " & _state.[GetType]().Name)
            End Set
        End Property

        Public Sub Request()
            _state.Handle(Me)
        End Sub

    End Class

End Namespace
